<?php
require_once './libraries/function.php';
$file_name = "data/films.txt";
$file_path = realpath($file_name);
$films = array();
$file = fopen($file_path, "r");
$data = fgets($file);
$rating = NULL;
while (!feof($file)) {
    $films[] = trim(fgets($file));
}
fclose($file);
?>

<form method="POST">
    <input type="text" name="rating">
    <input type="submit" name="submit" value="Search">
</form>
<?php if (isset($_POST['submit'])): ?>
    <?php $rating = (int) $_POST['rating']; ?>

<?php endif; ?>
<?php foreach ($films as $line): ?>
    <?php if (!empty($line)): ?>
        <?php $film = explode(";", $line); ?>
        <?php if (isset($_GET['uid'])): ?>
            <?php if ((int) $_GET['uid'] == (int) $film[4]): ?><br>
                <?php if (empty($rating)): ?>
                    <img src = 'public/films/<?php echo $film[1] ?>'>
                    <?php echo $film[2]; ?>
                    <?php echo $film[3]; ?>
                    <?php echo $film[4]; ?>
                    <br>
                <?php else: ?>
                    <?php if ($rating == (int) $film[4]): ?>
                        <img src = 'public/films/<?php echo $film[1] ?>'> 
                        <?php echo $film[2] ?>
                        <?php if (!empty($film[3])) echo $film[3] ?>
                        <br>
                    <?php endif; ?>
                <?php endif; ?>
            <?php endif; ?>
        <?php endif; ?>
    <?php endif; ?>
<?php endforeach; ?>

